package org.celery.modules.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.boot.tenant.TenantConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.goods.entity.Goods;
import org.celery.modules.goods.vo.GoodsVO;
import org.celery.modules.goods.wrapper.GoodsWrapper;
import org.celery.modules.goods.service.IGoodsService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品表 控制器
 *
 * @author Celery
 * @since 2021-10-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-goods/goods")
@Api(value = "商品表", tags = "商品表接口")
public class GoodsController extends BladeController {

	private final IGoodsService goodsService;
	private final IUserService userService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入goods")
	public R<GoodsVO> detail(Goods goods) {
		Goods detail = goodsService.getOne(Condition.getQueryWrapper(goods));
		return R.data(GoodsWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 商品表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goods")
	public R<IPage<GoodsVO>> list(Goods goods, Query query,BladeUser bladeUser) {
		QueryWrapper<Goods> queryWrapper = Condition.getQueryWrapper(goods);
		if (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) {
			queryWrapper.lambda().in(Goods::getTenantId, new ArrayList<String>() {{
				add(BladeConstant.ADMIN_TENANT_ID);
				add(bladeUser.getTenantId());
			}});
		}
		if (Func.isNotEmpty(goods.getName())) {
			queryWrapper.lambda().like(Goods::getName, goods.getName());
			goods.setName(null);
		}
		if (Func.isNotEmpty(goods.getSummary())) {
			queryWrapper.lambda().like(Goods::getSummary, goods.getSummary());
			goods.setSummary(null);
		}

		if (!SecureUtil.isAdministrator()) {
			queryWrapper.lambda().eq(Goods::getTenantId, bladeUser.getTenantId());

			List<User> allUnderUserList = userService.getUnderUserIdList(bladeUser);
			queryWrapper.lambda().in(
					Goods::getCreateUser,
					allUnderUserList.stream().map(User::getId).collect(Collectors.toList())
			);
		}

		queryWrapper.lambda().orderByAsc(Goods::getStatus).orderByDesc(Goods::getCreateTime);
		IPage<Goods> pages = goodsService.page(Condition.getPage(query), queryWrapper);
		return R.data(GoodsWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 商品表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入goods")
	public R<IPage<GoodsVO>> page(GoodsVO goods, Query query) {
		IPage<GoodsVO> pages = goodsService.selectGoodsPage(Condition.getPage(query), goods);
		return R.data(pages);
	}

	/**
	 * 新增 商品表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入goods")
	public R save(@Valid @RequestBody Goods goods) {
		return R.status(goodsService.save(goods));
	}

	/**
	 * 修改 商品表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入goods")
	public R update(@Valid @RequestBody Goods goods) {
		return R.status(goodsService.updateById(goods));
	}

	/**
	 * 新增或修改 商品表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入goods")
	public R submit(@Valid @RequestBody Goods goods, BladeUser bladeUser) {
		if (Func.isEmpty(goods.getId())) {
			goods.setTenantId(bladeUser.getTenantId());
		}
		return R.status(goodsService.saveOrUpdate(goods));
	}

	
	/**
	 * 删除 商品表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(goodsService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 修改状态
	 */
	@PostMapping("/status-change")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "修改状态", notes = "传入ids")
	public R statusChange(@ApiParam(value = "主键集合", required = true) @RequestParam String ids, @RequestParam Integer status) {
		return R.status(goodsService.update(Wrappers.<Goods>lambdaUpdate().set(Goods::getStatus, status).in(Goods::getId, Func.toLongList(ids))));
	}

	
}
